<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="持证人名称" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入持证人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证书名称" prop="certificateName">
        <el-input
          v-model="queryParams.certificateName"
          placeholder="请输入证书名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效日期">
        <el-date-picker
          v-model="daterangeExpirationDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:person_certificate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:person_certificate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:person_certificate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:person_certificate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="person_certificateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="持证人名称" align="center" prop="personName" width="100"/>
      <el-table-column label="证书名称" align="center" prop="certificateName" />
      <el-table-column label="签发日期" align="center" prop="issueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效日期" align="center" prop="expirationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="证书图片" align="center" prop="imagePath" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imagePath" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:person_certificate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:person_certificate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证书对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="持证人名称" prop="personId">
          <el-select
            v-model="form.personId"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="getPersonList"
            :loading="loading">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.personName"
              :value="item.personId">
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="证书名称" prop="certificateName" style="width: 300px">
          <el-input v-model="form.certificateName" placeholder="请输入证书名称" />
        </el-form-item>-->
        <el-form-item label="证书名称" prop="certificateDic">
          <el-select v-model="form.certificateDic" placeholder="请选择证书名称">
            <el-option
              v-for="dict in dict.type.person_certificate_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签发日期" prop="issueDate">
          <el-date-picker clearable
            v-model="form.issueDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签发日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效日期" prop="expirationDate">
          <el-date-picker clearable
            v-model="form.expirationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证书图片" prop="imagePath">
          <image-upload v-model="form.imagePath"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPerson_certificate, getPerson_certificate, delPerson_certificate, addPerson_certificate, updatePerson_certificate } from "@/api/business/person_certificate";
import {listProject_person} from "@/api/business/project_person";

export default {
  name: "Person_certificate",
  dicts: ['person_certificate_name'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证书管理表格数据
      person_certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 证书图片时间范围
      daterangeExpirationDate: [],
      options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personName: null,
        personId: null,
        certificateName: null,
        expirationDate: null,
      },
      //person查询参数
      queryPersonParams: {
        pageNum: 1,
        pageSize: 10,
        personName: null,
      },
      option: {
        personId: null,
        personName: null,
      },


      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personId: [
          { required: true, message: "持证人名称不能为空", trigger: "change" }
        ],
        certificateDic: [
          { required: true, message: "证书名称不能为空", trigger: "blur" }
        ],
        // issueDate: [
        //   { required: true, message: "签发日期不能为空", trigger: "blur" }
        // ],
        imagePath: [
          { required: true, message: "证书图片不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const tempPersonId = this.$route.params && this.$route.params.personId;
    if(tempPersonId!=null){
      this.queryParams.personId = tempPersonId;
    }
    this.getList();
  },
  methods: {
    /** 查询证书管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeExpirationDate && '' != this.daterangeExpirationDate) {
        this.queryParams.params["beginExpirationDate"] = this.daterangeExpirationDate[0];
        this.queryParams.params["endExpirationDate"] = this.daterangeExpirationDate[1];
      }
      listPerson_certificate(this.queryParams).then(response => {
        this.person_certificateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPersonList(query) {
      if (query !== '') {
        this.loading = true;
        this.queryPersonParams.personName = query;
        listProject_person(this.queryPersonParams).then(response => {
          this.loading = false;
          this.options = response.rows;
        });
      } else {
        this.options = [];
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        certificateId: null,
        personId: null,
        personName: null,
        certificateName: null,
        issueDate: null,
        expirationDate: null,
        imagePath: null,
        options: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeExpirationDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.certificateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加证书";
      this.loading = true;
      this.queryPersonParams.personName = '';
      listProject_person(this.queryPersonParams).then(response => {
        this.loading = false;
        this.options = response.rows;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      this.option.personName=row.personName;
      this.option.personId=row.personId;
      this.options.push(this.option);
      const certificateId = row.certificateId || this.ids
      getPerson_certificate(certificateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改证书";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.certificateId != null) {
            updatePerson_certificate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson_certificate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const certificateIds = row.certificateId || this.ids;
      this.$modal.confirm('是否确认删除证书？').then(function() {
        return delPerson_certificate(certificateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/person_certificate/export', {
        ...this.queryParams
      }, `person_certificate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
